<template>
	<!-- <uni-indexed-list :options="list" :show-select="true" @click="bindClick" /> -->
	<view>
		<uni-search-bar v-model="searchForm.sfindinfo" placeholder="请输入搜索条件" @confirm="search"
			@cancel="cancel" ></uni-search-bar>
		<view class="mediumButton">
			<u-button type="info" size="medium" shape="square" @click="onSubmit">提交</u-button>	
		</view>
		<uni-table :loading="loading" :data="procedureList" type="selection" border stripe emptyText="暂无更多数据" @selection-change="onSelectionChange" selection-type="checkbox" row-key="seq">
			<uni-tr>
				<uni-th align="center">序号</uni-th>
				<uni-th align="center">工序</uni-th>
				<uni-th align="center">工段</uni-th>
			</uni-tr>
			<uni-tr v-for="(item, index) in procedureList" :key="index" :key-value="item.id || item.seq || index">
				<uni-td align="center">{{item.seq}}</uni-td>
				<uni-td align="center">{{item.sProcedureName}}</uni-td>
				<uni-td align="center">{{item.sWorksectionName}}</uni-td>
			</uni-tr>

		</uni-table>
	</view>
</template>

<script>
	import fabricService from "@/api/auth/fabricService";
	export default {
		data() {
			return {
				loading: false,
				searchForm: {
					sfindinfo: ''
				},
				list: [{
					'data': []
				}],
				selectedRows: [],
				originalList: [],
				procedureList: []
			}
		},

		onLoad(query) {
			if (query.orderNo && query.styleNo) {
				this.loading = true;
				fabricService.workProcedureList({
					orderNo: query.orderNo,
					styleNo: query.styleNo
				}).then(res => {
					this.originalList = res;
					this.procedureList = res;
					this.loading = false;
				})
			}
		},
		methods: {
			search() {
				if (!this.searchForm.sfindinfo.trim()) {
					this.procedureList = this.originalList || [];
					return;
				}
				const searchText = this.searchForm.sfindinfo.toLowerCase();
				this.procedureList = (this.originalList || []).filter(item => {
					return item.sProcedureName.toLowerCase().includes(searchText);
				});
			},
			cancel() {
				this.searchForm.sfindinfo = "";
				if (!this.searchForm.sfindinfo.trim()) {
					this.procedureList = this.originalList || [];
					return;
				}
			},
			onSelectionChange(e) {
				if (e.detail) {
					if (e.detail.value && e.detail.value.length > 0) {
						this.selectedRows = e.detail.value;
					} 
					else if (e.detail.index && Array.isArray(e.detail.index)) {
						this.selectedRows = e.detail.index
							.map(idx => this.procedureList[idx])
							.filter(item => item !== undefined && item !== null);
					} else {
						this.selectedRows = [];
					}
				} else {
					this.selectedRows = [];
				}
			},
			onSubmit() {
			    if (!this.selectedRows || this.selectedRows.length === 0) {
			        uni.showToast({
			            title: '请选择至少一条数据',
			            icon: 'none',
			            duration: 2000
			        });
			        return;
			    }
			    
				uni.showToast({
				    title: `已选择${this.selectedRows.length}条数据`,
				    icon: 'success',
				    duration: 2000
				});
							    
			    // 这里可以根据需要返回选中的数据
			    // 例如通过事件传递给父页面
			    // uni.$emit('procedureSelected', this.selectedRows);
			    
			    // 或者返回上一页并传递数据
			    const pages = getCurrentPages();
			    const prevPage = pages[pages.length - 2];
			    if (prevPage) {
			    }
				uni.$emit('procedureSelected', this.selectedRows);
			    uni.navigateBack();

			    
			    // 如果需要返回上一页，可以取消下面的注释
			    // setTimeout(() => {
			    //     uni.navigateBack();
			    // }, 1500);
			}
		}
	}
</script>

<style>
	.mediumButton {
	  margin: 0px 10px;
	}

</style>